$(function() {

// -------------------------------------------------------------------------------------------------------------------------
//--------------------发布问题----------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------
	
  var post_id_status = JSON.parse(localStorage.getItem('post_id_status'));
  var timeoutHandler = null;
  if(post_id_status == true){
  
    //普通用户个人信息
    layer.msg('提交成功！');
    if(timeoutHandler){
           clearTimeout(timeoutHandler);
       }
       timeoutHandler = setTimeout(function(){
           location.href=jumpUrl+'/question/search';
           localStorage.removeItem('post_id_status');
           localStorage.removeItem('post_id');
      },2000);
  }

  $(".quesTitle").focus(function(){  // 获得焦点
		$(".quesTitle").css("border","1px solid #48b4ff")
	}).blur(function(){     // 失去焦点
		$(".quesTitle").css("border","1px solid #e8e8e8");
	});
	$(".textarea").focus(function(){  // 获得焦点
		$(".itxtarea").css("border","1px solid #48b4ff")
	}).blur(function(){     // 失去焦点
		$(".itxtarea").css("border","1px solid #e8e8e8");
	});

	//获取擅长领域的数据
    $.get(jumpUrl+'/lawyer/workscopes', function(data){
          
        }, "json").error(function(data) {
          var status_code = $.parseJSON(data.responseText);
          layer.msg(status_code.message);
        }).success(function(data) {
           var obj = data.data;
            var work_scope = obj;
            var src = '';
            $.each(work_scope, function(key, item) {
            	src+='<a value="'+item.key+'">'+item.value+'</a>';
            });
            $(".commit-type2 .type-list").append(src);


            $(".type-list a").click(function(){
    		    	var text = $(this).text();
    		    	var value = $(this).attr("value")
    		    	$(".commit-type1 .commit-title").text(text).css("color","#48b4ff");
    		    	$(".commit-type1 .commit-title").attr("value",value);
    		    	$(".commit-type2 .commit-title").text(text);
    		    	$(".commit-type2 .commit-title").attr("value",value);
    		    	$(".commit-type2 .type-list a").removeClass("selected");
    		    	$(this).addClass("selected");
    		    });
    });
    
    $(".commit-type1").click(function(){
		$(".commit-type2").css("display","block");
	});
	$(".commit-type2").click(function(){
    	$(".commit-type2").css("display","none");
	});
	$(".Error-closing").click(function(){
		$(".Error-prompt").css("display","none");
	});

	//提交咨询
  $(document).on( "click", ".subAdvisory", function(e) { 
      if(islogin == "1"){
        if (login_isLawyer_id == "1") {
          layer.msg("律师用户不能发布问题!");
            return false;
        };
            
      }
      var title = $.trim($("#title").val());
      var content = $.trim($("#content").val());
      var work_scope = $.trim($(".commit-type1 .commit-title").attr("value"));
      if(work_scope.length < 1){
      		$(".Error-prompt").css("display","block");
      		$(".textcon").text("请选择案件类型！")
          return false;
      }else if(title.length<2||title.length>100){
      		$(".Error-prompt").css("display","block");
      		$(".textcon").text("标题不能小于2个字或者大于100个字！")
          return false;
      }else if(content.length < 1){
      		$(".Error-prompt").css("display","block");
      		$(".textcon").text("内容不能为空！")
          return false;
      }else if(content.length < 10||content.length > 1000){
      		$(".Error-prompt").css("display","block");
      		$(".textcon").text("标题不能小于10个字或者大于1000个字！")
          return false;
      }else{
          var loading = layer.msg('加载中...', {icon: 16});
          var inquery = $("#inquery_create_form");
          $(this).attr("disabled","disabled");
          $.post(attrUrl+"/center/question/insert/question",{"title":title,"content":content,"cat_id":work_scope},
             function(data){
                $(':input',inquery).not(':button, :submit, :reset, :hidden').val(''); 
                $(".error-prompt").css("display","none");
                $(".commit-type1 .commit-title").attr("value","").text("选择咨询类别");
                var post_id = data.data.post_id;
                console.info(post_id);

                  localStorage.setItem("post_id", JSON.stringify(post_id));

                if(islogin == "" || islogin == null){
                  var post_id_status = false;
                  localStorage.setItem("post_id_status", JSON.stringify(post_id_status));
                  sessionStorage.setItem("goback", location.href);
                   $(".loginPopup").click();
                }else{
                    // });
                    layer.msg("发布成功");
                    window.location.href = jumpUrl+"/question/search";
                    $(".filename ul li").remove();
                    $("input:hidden[name='file_paths[]']").remove();
                    $("input:hidden[name='file_names[]']").remove();
                }
                layer.close(loading); 
                 $(".subAdvisory").removeAttr('disabled');
             }, "json").error(function(data) {
               var obj = $.parseJSON(data.responseText);
               layer.msg(obj.message);
             }).success(function(data){
              var obj = data.data;
             });
             
             return true;
      }
      stopDefault(e);
  });

})
